import {Component, OnInit} from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import {UsedStorePService} from '../used-store-p.service';

@Component({
  selector: 'app-logistics-records',
  templateUrl: './logistics-records.component.html',
  styleUrls: ['./logistics-records.component.scss']
})
export class LogisticsRecordsComponent implements OnInit {

  searchOptions = [
    { type: 1, labelText: '操作人', valueKey: 'operator', value: '' },
    {
      type: 2,
      labelText: '类型',
      valueKey: 'type',
      value: '',
      multiple: false,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [
        {value: 1, label: '入库'},
        {value: 2, label: '出库'}
      ]
    },
    { type: 1, labelText: '扫码结果', valueKey: 'result', value: '' },
    { type: 3, labelText: '操作时间', valueKey: ['operateTime', 'operateEndTime'], value: [], isTimeSelect: true }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  putOutObjs: any = {
    in: 0,
    out: 0
  };

  constructor(
    private msg: NzMessageService,
    private usedStorePService: UsedStorePService
  ) {
  }

  ngOnInit(): void {}

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.usedStorePService.logisticsRecords(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const {records, total} = data.list || {};

      if (records && records.length) {
        records.forEach((item: any) => {
          item.resultArrs = item.result ? item.result.split(',') : []
        })
      }

      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;

      for (let key in this.putOutObjs) {
        this.putOutObjs[key] = data[key] || 0;
      }
    } else {
      this.msg.error(message);
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }
}
